TAHAP PERTAMA(Persiapan)

Instalasi Package EBImage

Langkah ini adalah langkah yang wajib dilakukan agar fungsi dari EBImage itu dapat diakses dengan R Studio. Langkah tersebut dilakukan dengan menuliskan kode berikut ini :

if (!requireNamespace("BiocManager", quietly = TRUE))
    install.packages("BiocManager")
BiocManager::install("EBImage")
## Bioconductor version 3.12 (BiocManager 1.30.12), R 4.0.2 (2020-06-22)
## Installing package(s) 'EBImage'
## package 'EBImage' successfully unpacked and MD5 sums checked
## 
## The downloaded binary packages are in
##  C:\Users\LENOVO\AppData\Local\Temp\RtmpIrhVNb\downloaded_packages
## Old packages: 'Matrix', 'xfun'

TAHAP KEDUA

Set Lokasi Kerja, Memanggil Package, Import Gambar

Set Lokasi Kerja

Tahap ini adalah tahap dimana kita mengatur lokasi tempat gambar yang akan dieksekusi berada (biasanya satu folder dalam file project).

#set lokasi kerja
setwd("C:/Users/LENOVO/My Documents/KULIAHKU/SEMESTER 2/Linear Algebra/EbimageAdvanced/")

Memanggil Package

Memanggil package yang sudah diinstall di tahap 1, yaitu package EBImage.

#memanggil package
library(EBImage)
## Warning: package 'EBImage' was built under R version 4.0.3

Import Gambar

Dalam tahap ini kita perlu melakukan assign (import)pada fungsi readImage dengan judul photo yang sudah ditaruh di folder set kerja dan memberinya nama misal Image.

Image <- readImage('wisnu.jpg')

TAHAP KETIGA(Basic Operation)

Dalam tahap ini kita bisa melakukan operasi-operasi dasar, seperti menampilkan gambar, histsogram gambar ,writing.

Display Image

Method Browser

img = readImage("wisnu.jpg")
display(img, method="browser")

Display Image

display(img)

colorMat = matrix(rep(c("red","green", "#0000ff"), 25), 5, 5)
colorImg = Image(colorMat)
colorImg
## Image 
##   colorMode    : Color 
##   storage.mode : double 
##   dim          : 5 5 3 
##   frames.total : 3 
##   frames.render: 1 
## 
## imageData(object)[1:5,1:5,1]
##      [,1] [,2] [,3] [,4] [,5]
## [1,]    1    0    0    1    0
## [2,]    0    1    0    0    1
## [3,]    0    0    1    0    0
## [4,]    1    0    0    1    0
## [5,]    0    1    0    0    1
display(colorImg, interpolate=FALSE)

nuc = readImage(system.file("images", "nuclei.tif", package="EBImage"))
display(nuc, method = "raster", all = TRUE)

nuc = readImage(system.file("images", "nuclei.tif", package="EBImage"))
display(nuc, method = "raster")
## Only the first frame of the image stack is displayed.
## To display all frames use 'all = TRUE'.

Menapilkan Histogram Gambar

Histogram gambar adalah suatu grafik yang akan menahistmpilkan banyak hal bisa berupa kecerahan (brightness), kontas (contrast), warna (color) dari sebuah gambar. Cara menampilkan yaitu dengan cara berikut ini :

hist(Image)

Menampilkan Sifat Gambar

Langkah yang dilakukan untuk menampilkan sifat gambar yaitu merupakan spesifikasi gambar dan spesifikasi berupa matriks adalah dengan cara berikut ini:

print(Image)
## Image 
##   colorMode    : Color 
##   storage.mode : double 
##   dim          : 2544 1216 3 
##   frames.total : 3 
##   frames.render: 1 
## 
## imageData(object)[1:5,1:6,1]
##           [,1]      [,2]      [,3]      [,4]      [,5]      [,6]
## [1,] 0.5137255 0.5137255 0.5254902 0.5294118 0.5294118 0.5294118
## [2,] 0.5176471 0.5176471 0.5254902 0.5294118 0.5294118 0.5294118
## [3,] 0.5176471 0.5176471 0.5176471 0.5176471 0.5176471 0.5176471
## [4,] 0.5137255 0.5137255 0.5137255 0.5137255 0.5176471 0.5176471
## [5,] 0.5176471 0.5176471 0.5176471 0.5176471 0.5215686 0.5215686

Typography

Disini kita bisa menambahkan text pada gambar kita. Lokasi text bisa disesuaikan posisinya menggunakan koordinat x,y.

display(img)
text(x = 750, y = 6, label = "Believe in WONDERLAND", adj = c(0,1), col = "white", cex = 2)
text(x = 749, y = 3, label = "Believe in WONDERLAND", adj = c(0,1), col = "navy", cex = 2)

TAHAP KETIGA(Image Representation)

Dalam tahap ini kita bisa menggunakan fungsi yang ada di dalam library EBImage untuk menyimpan maupun memproses gambar. Semua fungsi yang ada di dalam EBImage dapat dipanggil berupa matrix dan array.

Image Data

str(Image)
## Formal class 'Image' [package "EBImage"] with 2 slots
##   ..@ .Data    : num [1:2544, 1:1216, 1:3] 0.514 0.518 0.518 0.514 0.518 ...
##   ..@ colormode: int 2

Array Image

Dalam Array IMmage Kita bisa menampilkan gambar tersebut berupa Array

dim(Image)
## [1] 2544 1216    3

Image Data Summary

Disini kita bisa menampilkan data-data penting dari gambar kita. Untuk menampilkannya senditi yaitu dengan menuliskan nama objek gambarnya saja

Image
## Image 
##   colorMode    : Color 
##   storage.mode : double 
##   dim          : 2544 1216 3 
##   frames.total : 3 
##   frames.render: 1 
## 
## imageData(object)[1:5,1:6,1]
##           [,1]      [,2]      [,3]      [,4]      [,5]      [,6]
## [1,] 0.5137255 0.5137255 0.5254902 0.5294118 0.5294118 0.5294118
## [2,] 0.5176471 0.5176471 0.5254902 0.5294118 0.5294118 0.5294118
## [3,] 0.5176471 0.5176471 0.5176471 0.5176471 0.5176471 0.5176471
## [4,] 0.5137255 0.5137255 0.5137255 0.5137255 0.5176471 0.5176471
## [5,] 0.5176471 0.5176471 0.5176471 0.5176471 0.5215686 0.5215686

TAHAP KEEMPAT(AKHIR)

Color Management

colorMode(Image) <- Grayscale
display(Image)
## Only the first frame of the image stack is displayed.
## To display all frames use 'all = TRUE'.

display(colorImg, interpolate=FALSE)

Manipulating Image

Negative Image

img_neg = max(img) - img
display( img_neg )

Brightness Image

img_comb = combine(
  img,
  img + 0.4,
  img * 3,
  img ^ 0.5
)

display(img_comb, all=TRUE)

Transpose Image

img_t = transpose(img)
display( img_t )

Spatial Transformation

Translate adalah dimana kita memindahkan bidang gambar dengan vektor dua dimensi yang sudah ditentukan, sehingga piksel yang masuk ke dalam area gambar disetel ke latar belakang dan piksel yang berada di area luar gambar dipotong.

img_translate = translate(img, c(150,-70))
display(img_translate)

Imagetr <- translate(rotate(Image, 45), c(50, 0))
display(Imagetr)
## Only the first frame of the image stack is displayed.
## To display all frames use 'all = TRUE'.

Dibawah ini kita bisa melakukan resize pada gambar kita

img_resize = resize(img, w=450, h=450)
display(img_resize )

Dibawah ini kita juga bisa melakukan pencerminan terhadap gambar kita

img_flip = flip(img)
img_flop = flop(img)

display(combine(img_flip, img_flop), all=TRUE)

Filtering

High Pass Filtering

fhi = matrix(1, nrow = 3, ncol = 3)
fhi[2, 2] = -8
img_fhi = filter2(img, fhi)
display(img_fhi)

Linear Filtering

w = makeBrush(size=31, shape='gaussian', sigma=5)
plot(w[nrow(w)+1/2, ], ylab="2", xlab="", cex=0.7)

img_flo = filter2(img, w)
display(img_flo)

Median Filtering

l = length(img)
n = l/10
pixels = sample(l, n)
img_noisy = img
img_noisy[pixels] = runif(n, min=0, max=1)
display(img_noisy)

Morphological Operation

shapes = readImage(system.file('images', 'shapes.png', package = 'EBImage'))
logo = shapes[110:512,1:130]
display(logo)

kern = makeBrush(5, shape='diamond')
display(kern, interpolate=FALSE)

logo_erode= erode(logo, kern)
logo_dilate = dilate(logo, kern)

display(combine(logo_erode, logo_dilate), all=TRUE)

Image Manipulation

Disini kita bisa membuat gambar inti dan badan sel. Untuk memvisualisasikan sel, dengan melapisi gambar-gambar ini sebagai saluran hijau dan biru dari gambar berwarna palsu.

nuc = readImage(system.file('images', 'nuclei.tif', package='EBImage'))
cel = readImage(system.file('images', 'cells.tif', package='EBImage'))

cells = rgbImage(green=1.5*cel, blue=nuc)
display(cells, all = TRUE)

Pertama yang harus dilakukan adalah segmentasi menggunakan thresh, fillHull, bwlabel dan opening.

nmask = thresh(nuc, w=10, h=10, offset=0.05)
nmask = opening(nmask, makeBrush(5, shape='disc'))
nmask = fillHull(nmask)
nmask = bwlabel(nmask)

display(nmask, all=TRUE)

Kemudian, kita gunakan inti tersegmentasi sebagai benih dalam segmentasi Voronoi dan sitoplasma

ctmask = opening(cel>0.1, makeBrush(5, shape='disc'))
cmask = propagate(cel, seeds=nmask, mask=ctmask)

display(ctmask, all=TRUE)

Terakhir, kita visualisasikan segmentasi pada objek, menggunakan paintObject

segmented = paintObjects(cmask, cells, col='#ff00ff')
segmented = paintObjects(nmask, segmented, col='#ffff00')

display(segmented, all=TRUE)